package com.abel.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import java.text.*;
import com.abel.web.view.MilitaryServiceViewBean;

public class MilitaryServiceViewValidator implements Validator {

	@SuppressWarnings("rawtypes")
	@Override
	public boolean supports(Class clazz) {
		
		return MilitaryServiceViewBean.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {
		// TODO Auto-generated method stub
		MilitaryServiceViewBean viewBean = (MilitaryServiceViewBean)obj;
		
		if (viewBean.getInductionDate() != null && viewBean.getInductionDate() != "") {
			try{
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
			    dateFormat.parse(viewBean.getInductionDate()); 
			}
			catch (Exception e) {
				errors.rejectValue("inductionDate", "error.null",null,"Invalid Induction Date");
			}
		}
		
		if (viewBean.getDischargeDate() != null && viewBean.getDischargeDate() != "") {
			try{
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
			    dateFormat.parse(viewBean.getDischargeDate()); 
			}
			catch (Exception e) {
				errors.rejectValue("inductionDate", "error.null",null,"Invalid Discharge Date");
			}
		}
		if (viewBean.getInductionDate() != null 
				&& viewBean.getInductionDate() != ""
					&& (viewBean.getBranch() == null
				|| viewBean.getBranch().equals(""))) {
			errors.rejectValue("branch", "error.null",null,"Branch is a required field");
		}
		if (viewBean.getInductionDate() != null 
				&& viewBean.getInductionDate() != ""
					&& (viewBean.getRank() == null
				|| viewBean.getRank().equals(""))) {
			errors.rejectValue("rank", "error.null",null,"Rank is a required field");
		}
		if (viewBean.getInductionDate() != null 
				&& viewBean.getInductionDate() != ""
					&& (viewBean.getDuties() == null
				|| viewBean.getDuties().equals(""))) {
			errors.rejectValue("duties", "error.null",null,"Duties cannot be left blank");
		}
	}
	
}